package com.atguigu.tingshu.search.service.impl;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.CategoryFeignClient;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.search.AlbumInfoDetailVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class ItemServiceImpl implements ItemService {

    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;

    @Autowired
    private CategoryFeignClient categoryFeignClient;

    @Autowired
    private  UserInfoFeignClient userInfoFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor ;

    @Autowired
    private RedissonClient redissonClient ;



    @Override
    public AlbumInfoDetailVo getAlbumInfoDetail(Long albumId) {

        // 通过布隆过滤器判断专辑id是否存在
        RBloomFilter<Long> bloomFilter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
        if(!bloomFilter.contains(albumId)) {
            log.info("布隆过滤器判断该专辑的id是不存在的，直接返回， albumId：{} " , albumId );
            return null ;
        }

        //首先创建一个详情对象
        AlbumInfoDetailVo albumInfoDetailVo   = new AlbumInfoDetailVo();

        CompletableFuture<AlbumInfo> cf1 = CompletableFuture.supplyAsync(() -> {
            //1.根据专辑的id查询专辑的统计数据
            Result<AlbumInfo> albumInfoResult = albumInfoFeignClient.getAlbumInfo(albumId);
            AlbumInfo albumInfo = albumInfoResult.getData();
            albumInfoDetailVo.setAlbumInfo(albumInfo);
            return albumInfo;
        }, threadPoolExecutor);

        CompletableFuture<Void> cf2 = CompletableFuture.runAsync(() -> {
            //2.根据专辑的三级分类数据查找专辑的分类数据
            Result<AlbumStatVo> albumStatVoResult = albumInfoFeignClient.findAlbumStatByAlbumId(albumId);
            AlbumStatVo albumStatVo = albumStatVoResult.getData();
            albumInfoDetailVo.setAlbumStatVo(albumStatVo);
        }, threadPoolExecutor);

        CompletableFuture<Void> cf3 = cf1.thenAcceptAsync((albumInfo) -> {
            Long category3Id = albumInfo.getCategory3Id();
            //3.3、根据专辑的三级分类数据查找专辑的分类数据
            Result<BaseCategoryView> categoryViewResult = categoryFeignClient.findCategoryByC3Id(category3Id);
            BaseCategoryView baseCategoryView  = categoryViewResult.getData();
            albumInfoDetailVo.setBaseCategoryView(baseCategoryView);
        },threadPoolExecutor);

        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        CompletableFuture<Void> cf4 = cf1.thenAcceptAsync((albumInfo) -> {
            RequestContextHolder.setRequestAttributes(requestAttributes);
            //4.根据专辑的用户的id查询专辑的作者数据
            Long userId = albumInfo.getUserId();
            Result<UserInfoVo> userInfoVoResult = userInfoFeignClient.findUserInfoVoByUserId(userId);
            UserInfoVo userInfoVo = userInfoVoResult.getData();
            albumInfoDetailVo.setAnnouncer(userInfoVo);
        },threadPoolExecutor);

        CompletableFuture.allOf(cf1,cf2,cf3,cf4).join();

        //返回对象
        return albumInfoDetailVo;
    }
}
